package com.dragon.common.modules.file.web.controller;

import com.chris.dev.base.libs.v1.http.params.ResourceParam;
import com.chris.dev.base.libs.v1.http.results.ListResult;
import com.chris.dev.base.libs.v1.http.results.ResourceResult;
import com.dragon.common.modules.file.service.def.FileService;
import com.dragon.common.modules.file.web.api.FileApi;
import com.dragon.common.modules.file.web.model.result.Base64Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Chris Chan
 * Create On 2025/8/26 下午7:10
 * Use for: 文件系统 接口实现
 * Explain:
 */
@RestController
@RequestMapping("file")
public class FileController implements FileApi {
    @Autowired
    private FileService fileService;

    @Override
    public ResourceResult upload(@RequestPart("file") MultipartFile file) {
        return fileService.upload(file);
    }

    @Override
    public ResourceResult uploadWithName(
            @RequestParam("fileName") String fileName,
            @RequestPart("file") MultipartFile file) {
        return fileService.uploadWithName(fileName, file);
    }

    @Override
    public ResourceResult uploadFile(
            @RequestParam("mode") int mode,
            @RequestPart("file") MultipartFile file) {
        return fileService.uploadFile(mode, file);
    }

    @Override
    public ListResult<ResourceResult> uploadFiles(
            @RequestParam("mode") int mode,
            @Parameter(
                    description = "上传的文件",
                    required = true,
                    schema = @Schema(type = "string", format = "binary"),
                    in = ParameterIn.DEFAULT
            )
            @RequestPart("files") MultipartFile... files) {
        return ListResult.of(fileService.uploadFiles(mode, files));
    }

    @Override
    public ResourceResult uploadBase64(@RequestBody @Valid ResourceParam param) {
        return fileService.uploadBase64(param);
    }

    @Override
    public Base64Result fileToBase64(@RequestPart("file") MultipartFile file) {
        return fileService.fileToBase64(file);
    }
}
